package com.dbManage.page.Tag;

import com.dbManage.dao.CustomTagDao;
import com.dbManage.entity.CustomTag;
import com.dbManage.entity.Message;
import com.dbManage.entity.User;
import com.dbManage.exception.CommonException;
import com.dbManage.tools.SessionTool;
import com.wsse.common.tool.Console;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class CustomTagController {
    @Resource
    CustomTagDao customTagDao;

    @Resource
    CustomTagService customTagService;

    /**
     * 自定义标签列表
     * @param model
     * @return
     */
    @RequestMapping("/customTagList")
    public String customTagList(Model model){
        User user= SessionTool.getUser();
        model.addAttribute("list", customTagDao.findAllCustomTag(user.getId()));
        return "customTagList";
    }

    /**
     * 标签添加页面
     * @param
     * @return
     */
    @RequestMapping("/addCustomTagView")
    public String addCustomTagView() {
        return "CustomTagAdd";
    }

    /**
     * 添加自定义标签
     * @param tag
     * @return
     */
    @RequestMapping("/addCustomTag")
    @ResponseBody
    public Message addCustomTag(CustomTag tag) {
        try {
            customTagService.addCustomTag(tag);
        } catch (CommonException e) {
            return new Message(Message.STATUS_FAILED, e.getMessage());
        }
        return new Message(Message.STATUS_SUCCESS, "添加成功");
    }

    /**
     * 标签修改页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/updateTagView")
    public String updateTagView(Model model, int id) {
        CustomTag ct = customTagDao.findCustomTagById(id);
        if("".equals(ct.getPs())||ct.getPs()==null)ct.setPs("");
        model.addAttribute("customTag", ct);
        return "customTagEdit";
    }

    /**
     * 修改标签
     * @param tag
     * @return
     */
    @RequestMapping("/updateCustomTag")
    @ResponseBody
    public Message updateCustomTag(CustomTag tag) {
        tag.setTag(tag.getTag().toLowerCase());
        customTagDao.updateCustomTag(tag);
        return new Message(Message.STATUS_SUCCESS, "修改成功");
    }

    /**
     * 删除标签
     * @param id
     * @return
     */
    @RequestMapping("/deleteCustomTag")
    @ResponseBody
    public Message deleteCustomTag(int id) {
        customTagDao.deleteCustomTagById(id);
        return new Message(Message.STATUS_SUCCESS, "删除成功");
    }

}
